package com.decoration.basic.controller.api;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.decoration.basic.BasicController;
import com.decoration.basic.entity.DecCtctionInMsg;
import com.decoration.basic.entity.DecCtctionPo;
import com.decoration.basic.findCondition.DecCtctionFindCondition;
import com.decoration.basic.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.*;

import com.decoration.basic.controller.CommonController;
import com.decoration.basic.entity.DecorationUser;
import com.decoration.basic.entity.JsonResult;
import com.decoration.basic.service.DecCtctionService;
import com.decoration.basic.service.DecUserService;
import com.decoration.basic.util.Global;

@RestController
@RequestMapping("/api")
public class DecCtctionController extends BasicController{
	private static Log log = LogFactory.getLog(DecCtctionController.class);

	@Resource
	private DecCtctionService decCtctionService;

	@Resource
	private DecUserService decUserService;

	// 用户页面-获取项目列表
	@RequestMapping(value = "/ctclist", method = RequestMethod.POST)
	public JsonResult ctclist(JsonResult jsonResult, @RequestBody DecCtctionFindCondition inpo, HttpSession session) {

		//1.校验用户信息
		log.info("校验用户信息");
		String userID = null;
		try {
			userID = (String) session.getAttribute(Global.SESSION_USER_ID);
		} catch (Exception e) {
//			System.out.println(e);
			log.error(e);
		}

		if( userID == null){
			jsonResult.setCode(JsonResult.FAILED_ACCOUNT_ABNORMAL);
			jsonResult.setMsg("查询用户信息失败，请联系管理员");
			return jsonResult;
		}

		DecorationUser user = decUserService.findById(userID);
		if (user == null || (user.getPerAccount() != null && user.getPerAccount().length() < 1)) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("无登录信息，请重新登录");

			return jsonResult;
		}
		//2. 校验项目状态
		if ( null == inpo.getCtcProgress() ) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("项目状态不能为空");
			return jsonResult;
		}

//		3.获取项目列表
		setPagerInfo(inpo);
		try{
			jsonResult = decCtctionService.searchCtcList(user, inpo);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("获取项目列表异常");
			return jsonResult;
		}

		return jsonResult;
	}

	// 用户页面-获取项目内容
	@RequestMapping(value = "/ctcbyid", method = RequestMethod.POST)
	public JsonResult ctcbyid(JsonResult jsonResult, HttpServletRequest request) {

		Map<String, String> map = CommonController.getRequestBodyMap(request);
		String uid = map.get("ctcId");

		DecCtctionInMsg result = decCtctionService.searchCtcInfo(uid);
		if (result == null || result.getCtcid().equals("")) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("获取项目信息失败");
		}else {
			jsonResult.setCode(JsonResult.SUCCESS_CODE);
			jsonResult.setMsg("获取项目信息成功");
			jsonResult.setCount(1);
			jsonResult.setData(result);
		}
		return jsonResult;
	}

}
